package com.ruoyi.ldzlsb.controller;

import java.util.List;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.ldzlsb.domain.SbMaintenanceItem;
import com.ruoyi.ldzlsb.service.ISbMaintenanceItemService;

/**
 * 保养项目Controller
 * 
 * @author anli
 * @date 2025-06-12
 */
@RestController
@RequestMapping("/sb/maintenance/item")
public class SbMaintenanceItemController extends BaseController
{
    @Autowired
    private ISbMaintenanceItemService sbMaintenanceItemService;

    /**
     * 查询保养项目列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SbMaintenanceItem sbMaintenanceItem)
    {
        startPage();
        List<SbMaintenanceItem> list = sbMaintenanceItemService.selectSbMaintenanceItemList(sbMaintenanceItem);
        return getDataTable(list);
    }

    /**
     * 根据配置ID查询保养项目列表
     */
    @GetMapping("/list/{configId}")
    public TableDataInfo listByConfigId(@PathVariable("configId") Long configId)
    {
        startPage();
        List<SbMaintenanceItem> list = sbMaintenanceItemService.selectSbMaintenanceItemByConfigId(configId);
        return getDataTable(list);
    }

    /**
     * 导出保养项目列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, SbMaintenanceItem sbMaintenanceItem)
    {
        List<SbMaintenanceItem> list = sbMaintenanceItemService.selectSbMaintenanceItemList(sbMaintenanceItem);
        ExcelUtil<SbMaintenanceItem> util = new ExcelUtil<SbMaintenanceItem>(SbMaintenanceItem.class);
        util.exportExcel(response, list, "保养项目数据");
    }

    /**
     * 获取保养项目详细信息
     */
    @GetMapping(value = "/{itemId}")
    public AjaxResult getInfo(@PathVariable("itemId") Long itemId)
    {
        return success(sbMaintenanceItemService.selectSbMaintenanceItemByItemId(itemId));
    }

    /**
     * 新增保养项目
     */
    @PostMapping
    public AjaxResult add(@RequestBody SbMaintenanceItem sbMaintenanceItem)
    {
        String username = SecurityUtils.getUsername();
        sbMaintenanceItem.setCreateBy(username);
        sbMaintenanceItem.setUpdateBy(username);
        return toAjax(sbMaintenanceItemService.insertSbMaintenanceItem(sbMaintenanceItem));
    }

    /**
     * 批量新增保养项目
     */
    @PostMapping("/batch")
    public AjaxResult addBatch(@RequestBody List<SbMaintenanceItem> items)
    {
        String username = SecurityUtils.getUsername();
        for (SbMaintenanceItem item : items) {
            item.setCreateBy(username);
            item.setUpdateBy(username);
        }
        return toAjax(sbMaintenanceItemService.batchInsertSbMaintenanceItems(items));
    }

    /**
     * 修改保养项目
     */
    @PutMapping
    public AjaxResult edit(@RequestBody SbMaintenanceItem sbMaintenanceItem)
    {
        return toAjax(sbMaintenanceItemService.updateSbMaintenanceItem(sbMaintenanceItem));
    }

    /**
     * 删除保养项目
     */
    @DeleteMapping("/{itemIds}")
    public AjaxResult remove(@PathVariable Long[] itemIds)
    {
        return toAjax(sbMaintenanceItemService.deleteSbMaintenanceItemByItemIds(itemIds));
    }
    
    /**
     * 批量保存保养项目
     */
    @PostMapping("/batchSave/{configId}")
    public AjaxResult batchSaveMaintenanceItems(@PathVariable("configId") Long configId, @RequestBody List<SbMaintenanceItem> items, String username)
    {
        return toAjax(sbMaintenanceItemService.batchSaveMaintenanceItems(configId, items, username));
    }
} 